/**
 * @name ResultsAdapter
 * @version 0.1.403ra Alpha, MASS Results Adapter for Moisture Flux,3rd commit
 * @date 9/18/2013
 * @author Niko Simonson
 * @author Brett Yasutake
 *
 */

import java.io.IOException;
import ucar.ma2.*;
import ucar.nc2.*;
import java.util.*;
import java.util.AbstractMap.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ResultsAdapter {
    // placeholder information
    public static final int DOUBLE = 0;
    
    // variables
    private int type;  // every variable must be the same type
    private String name;
    SimpleEntry<String, Integer[]>[] vars;  // name of variables, ranges of variable
    private NetcdfFileWriteable currentFile;
    
    public ResultsAdapter(int varType, String fName, SimpleEntry<String, Integer[]>... variables) {
        vars = variables;
        type = varType;
        name = fName;
        
        create();
    }
    
    private void create() {
        try {
            currentFile = NetcdfFileWriteable.createNew(name);
            int dimLength = vars[0].getValue().length;
            Dimension[] currentDims = new Dimension[dimLength];
            

            for (int i = 0; i < dimLength; ++i) {
                currentDims[i] = currentFile.addDimension("Dimension" + i, vars[0].getValue()[i]);
            }
            
            String[] varNames = new String[vars.length];
            for (int i = 0; i < vars.length; ++i) {
                varNames[i] = vars[i].getKey();
                currentFile.addVariable(varNames[i], getType(), currentDims);
            }
            
            currentFile.create();
            currentFile.close();
            
            
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        
    }
    
    private DataType getType(){
        switch(type) {
            case DOUBLE:
                return DataType.DOUBLE;
            default:
                return DataType.DOUBLE;
        }
    }
    
    public static void log(double[] results){
        
    }
    
    public void lg(double[] results) {
//        try {
//            NetcdfFileWriteable.openExisting(name);
//            
//            ArrayDouble.D1 d1;
//            currentFile.getDimensions().get(0).getLength();
//            //currentFile.wr
//        } catch (IOException ex) {
//            Logger.getLogger(ResultsAdapter.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }
    

    
    public void log2DVar(String targetName, double[][] values) {
        Integer[] ranges = new Integer[2];
        
        for (int i = 0; i < vars.length; ++i) {
            if (vars[i].getKey() == targetName) {
                
                ranges = vars[i].getValue();
            }
        }
        //ArrayDouble.D2 inValues = new ArrayDouble.;
        ArrayDouble.D2 inValues = new ArrayDouble.D2(ranges[0], ranges[1]);
        
        for (int i = 0; i < ranges[0]; ++i) {
            for (int j = 0; j < ranges[1]; ++j) {
                inValues.set(i, j, values[i][j]);
            }
        }
        
        try {
            currentFile = NetcdfFileWriteable.openExisting(name);
            currentFile.write(targetName, inValues);
            
            currentFile.close();
        } catch (IOException ex) {
            Logger.getLogger(ResultsAdapter.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (InvalidRangeException ire) {
            ire.printStackTrace();;
        }

    }
    
    public void finish(){
        
    }
}
